package com.sky.wsp.rabbitmq.exchange;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 发布订阅模式的配置,包括两个队列和对应的订阅者,发布者的交换机类型使用fanout(子网广播),两根网线binding用来绑定队列到交换机
 */
@Configuration
public class FanoutExchangeConfig {
    public static final String EXCHANGE_NAME = "my_fanout";
    public static final String QUEUE_NAME_1 = "fanout_queue_1";
    public static final String QUEUE_NAME_2 = "fanout_queue_2";

    @Bean
    public Queue myQueue1() {
        Queue queue = new Queue(QUEUE_NAME_1);
        return queue;
    }

    @Bean
    public Queue myQueue2() {
        Queue queue = new Queue(QUEUE_NAME_2);
        return queue;
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        FanoutExchange fanoutExchange = new FanoutExchange(EXCHANGE_NAME);
        return fanoutExchange;
    }

    @Bean
    public Binding bindingFanout1() {
        Binding binding = BindingBuilder.bind(myQueue1()).to(fanoutExchange());
        return binding;
    }

    @Bean
    public Binding bindingFanout2() {
        Binding binding = BindingBuilder.bind(myQueue2()).to(fanoutExchange());
        return binding;
    }

}
